﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using agvcontroller.Common;
using agvcontroller.Common.DataBase;
using agvcontroller.Log;

namespace agvcontroller.Alarm
{
    delegate void deleSetAlarm(object sender , int alarmid);
    delegate void deleReleaseAlarm(object sender, int alarmid);

    public struct stAlarm
    {
        public int ID;
        public string Text;
        public int Code;
        public string Level;
    }

    class CAlarmManager
    {
        public event deleSetAlarm onSetAlarm;
        public event deleReleaseAlarm onReleaseAlarm;

        CAGVCDB AGVCDB;
        Dictionary<int, stAlarm> AlarmList = new Dictionary<int,stAlarm>();

        Logger Log = new Logger("Alarm");

        public int InitModule()
        {
            AGVCDB = CAGVCDB.getInstance();

            GetDataFromDB();

            return CReturnCode.SUCCESS;
        }

        int GetDataFromDB()
        {
            DataTable table = AGVCDB.AGVCDataSet.Tables["AlarmList"];

            stAlarm alarm;
            foreach (DataRow row in table.Rows)
            {
                alarm = new stAlarm();

                alarm.ID = Convert.ToInt32(row["ID"]);
                alarm.Code = Convert.ToInt32(row["Code"]);
                alarm.Level = Convert.ToString(row["Level"]);
                alarm.Text = Convert.ToString(row["Text"]);

                AlarmList.Add(alarm.ID,alarm);
            }
            return CReturnCode.SUCCESS;
        }

        int SetDatatoDB()
        {

            return CReturnCode.SUCCESS;
        }

        public void SetAlarm(int alarmid)
        {
            if (onSetAlarm != null)
            {
                onSetAlarm(this, alarmid);
            }
        }

        public void ReleaseAlarm(int alarmid)
        {
            if (onReleaseAlarm != null)
            {
                onReleaseAlarm(this, alarmid);
            }
        }

        public bool GetLevelByID(int alarmID)
        {
            return false;
        }
    }
}
